package edu.towson.cosc.cosc455.ryanhoran;

public class SemAnalyzer {
	
	public node[] VAR;
	public int i = 0;
	
	public void translate(){
		String token;
		while(!(globalVar.parseTree.empty())){
			token = globalVar.parseTree.pop();
			//check for which token it is (This will be a long process)
			//First check the variables
			if (token.equalsIgnoreCase("#start define")){
				String name;
				String val;
				token = globalVar.parseTree.pop();
				token = globalVar.parseTree.pop();
				name = token;
				token = globalVar.parseTree.pop();
				token = globalVar.parseTree.pop();
				val = token;
				addVar(name,val);
				token = globalVar.parseTree.pop();
			} else if (token.equalsIgnoreCase("#start use")){
				String name;
				name = globalVar.parseTree.pop();
				useVar(name);
				globalVar.parseTree.pop();
			} else if (token.equalsIgnoreCase("#start Document")){
				globalVar.html.push("<globalVar.html>");
			} else if (token.equalsIgnoreCase("#end document")){
				globalVar.html.push("</globalVar.html>");
			} else if (token.equalsIgnoreCase("#start head")){
				globalVar.html.push("<head>");
			} else if (token.equalsIgnoreCase("#end head")){
				globalVar.html.push("</head>");
			} else if (token.equalsIgnoreCase("#start title")){
				globalVar.html.push("<title>");
			} else if (token.equalsIgnoreCase("#end title")){
				globalVar.html.push("</title>");
			} else if (token.equalsIgnoreCase("#start paragraph")){
				globalVar.html.push("<p>");
			} else if (token.equalsIgnoreCase("#end paragraph")){
				globalVar.html.push("</p>");
			} else if (token.equalsIgnoreCase("#start bold")){
				globalVar.html.push("<b>");
			} else if (token.equalsIgnoreCase("#end bold")){
				globalVar.html.push("<b>");
			} else if (token.equalsIgnoreCase("#start italics")){
				globalVar.html.push("<i>");
			} else if (token.equalsIgnoreCase("#end italics")){
				globalVar.html.push("</i>");
			} else if (token.equalsIgnoreCase("#start list")){
				globalVar.html.push("<ul>");
			} else if (token.equalsIgnoreCase("#end list")){
				globalVar.html.push("</ul>");
			} else if (token.equalsIgnoreCase("#start item")){
				globalVar.html.push("<li>");
			} else if (token.equalsIgnoreCase("#end item")){
				globalVar.html.push("</li>");
			} 
			//note the special translations with the audio and video
			else if (token.equalsIgnoreCase("#start audio")){
				globalVar.html.push("<audio controls><source src= '");
			} else if (token.equalsIgnoreCase("#end audio")){
				globalVar.html.push("'></audio>");
			} else if (token.equalsIgnoreCase("#start video")){
				globalVar.html.push("iframe = '");
			} else if (token.equalsIgnoreCase("#end video")){
				globalVar.html.push("'/>");
			} else if (token.equalsIgnoreCase("#newline")){
				globalVar.html.push("<br>");
			} else {
				globalVar.html.push(token);
			}		
		} new InputOutput().write();
		
	}
	//Begin Variable Resolution
	public void addVar(String addname, String val){
		VAR[i].name = addname;
		VAR[i].val = val;
		i++;
	}
	
	public void useVar(String chkname){
		int j;
		for(j = 0; j < i; j++){
			if (VAR[i].name == chkname){
				globalVar.html.push(VAR[i].val);
				return;
			}
		} error("Variable " + chkname + "not defined.");
	}
	//End Variable Resolution
	
	//Exception Handling
	public void error(String message){
		System.out.printf("Error in Semantics.%n%s%n Program ClosinglobalVar.", message);
		System.exit(0);
	}
}

class node{
	public String val;
	public String name;
}